import 'dart:convert';


import 'package:wallet_utils/local_store_info_bean.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'bean/login_bean.dart';
import 'local_store_info_bean.dart';

class LoginUtils {
  //第一次登录展示用户协议
  //保存会员信息
  static saveAgreementBool(bool agreement_bool) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.setBool("agreement_bool", agreement_bool);
  }

  //获取会员信息
  static Future<bool> getAgreementBool() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getBool("agreement_bool") ?? true;
  }

  //判断是否登录
  static Future<bool> isLogin() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    String? loginStr = preferences.getString("loginBean");
    if (loginStr == null || loginStr.isEmpty) {
      return false;
    } else {
      return true;
    }
  }

  //保存登录信息
  static void saveLoginBean(String loginBeanStr) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    await preferences.setString("loginBean", loginBeanStr);
  }

  //获取登录信息Json
  static getLoginBean() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return json.decode(preferences.getString("loginBean")!);
  }

//获取登录信息对象
  static getLoginBeanInfo() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return LoginBean.fromJson(json.decode(preferences.getString("loginBean")!));
  }

  //=================== UserId ===================
  /// businessLoginId == userId
  /// @params businessLoginId:老版本叫法
  /// @params userId:新版本叫法 (新版本最好用这个)

  // 保存businessLoginId信息
  static void saveBusinessLoginId(String businessLoginId) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    await preferences.setString("businessLoginId", businessLoginId);
  }

  //获取businessLoginId信息
  static getBusinessLoginId() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getString("businessLoginId");
  }

  //获取businessId信息
  static getBusinessId() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getString("businessId");
  }

  //获取marketId信息
  static getMarketId() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getString("marketId");
  }

  //获取marketName信息
  static getMarketName() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getString("marketName");
  }

  //获取用户名称
  static getUserName() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getString("userName");
  }

  // 保存userId信息
  static void saveUserId(String userId) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    await preferences.setString("userId", userId);
  }

  //获取userId信息
  static getUserId() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getString("userId");
  }

  //===================UserId ===================

  //退出登录
  static loginOut() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.setString("loginBean", ""); //登录返回数据对象
    preferences.setString("token", ""); //个人token验证
    LoginUtils.setIndexStatus(0);
  }

  //保存选择的店铺信息
  /// mode 0线上电商 1实体门店 2写字楼
  static saveStoreInfo({required mode, required String storeInfoStr, required storeId}) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    if (mode == 0) {
      //线上电商
      preferences.setInt("OnLineStoreID", storeId);
      return preferences.setString("OnLineStoreInfo", storeInfoStr);
    } else if (mode == 1) {
      //实体门店
      preferences.setInt("OffLineStoreID", storeId);
      return preferences.setString("OffLineStoreInfo", storeInfoStr);
    } else if (mode == 2) {
      //写字楼
      preferences.setInt("OfficeStoreID", storeId);
      return preferences.setString("OfficeStoreInfo", storeInfoStr);
    }
    // return mode == 0?preferences.setString("OffLineStoreInfo",storeInfoStr);
  }

  //获取线上电商店铺信息
  static getOnLineStoreInfo() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return json.decode(preferences.getString("OnLineStoreInfo")!);
  }

  //获取实体门店店铺信息
  static getOffLineStoreInfo() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return json.decode(preferences.getString("OffLineStoreInfo")!);
  }

  //获取写字楼信息
  static getOfficeStoreInfo() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return json.decode(preferences.getString("OfficeStoreInfo")!);
  }

  ///----------------ID信息存储-----------------
  //保存当前线上电商店铺id
  static saveOnLineStoreId({required storeId}) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.setInt("OnLineStoreID", storeId);
  }

  //获取当前线上电商店铺id
  static getOnLineStoreId() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getInt("OnLineStoreID");
  }

  //保存当前实体门店店铺id
  static saveOffLineStoreId({required storeId}) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.setInt("OffLineStoreID", storeId);
  }

  //获取当前实体门店店铺ID
  static getOffLineStoreId() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getInt("OffLineStoreID");
  }

  //保存当前写字楼id
  static saveOfficeStoreId({required storeId}) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.setInt("OfficeStoreID", storeId);
  }

  //获取当前写字楼ID
  static getOfficeStoreId() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getInt("OfficeStoreID");
  }

  //店铺相关信息bean类存储（兼容老模块接口字段）
  static saveBusinessInfo(LocalStoreInfoBean localStoreInfo) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.setString('businessId', localStoreInfo.businessId ?? '');
    preferences.setString('businessLoginId', localStoreInfo.businessLoginId ?? '');
    preferences.setString('marketId', localStoreInfo.marketId ?? '');
    preferences.setString('marketName', localStoreInfo.marketName ?? '');
    preferences.setString('businessName', localStoreInfo.businessName ?? '');
    preferences.setString('userPhone', localStoreInfo.userPhone ?? '');
    preferences.setString('userName', localStoreInfo.userName ?? '');
    preferences.setInt('isAdmin', localStoreInfo.isAdmin ?? 0);
    preferences.setString('mode', localStoreInfo.mode ?? '');
    preferences.setString('authNo', localStoreInfo.authNo ?? '');
    preferences.setInt('openStoreStatus', localStoreInfo.openStoreStatus ?? 0);
  }

  //获取店铺名字
  static getBusinessName() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getString("businessName");
  }

  //获取登录手机号
  static getUserPhone() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getString("userPhone");
  }

  //获取token
  static getToken() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getString("token");
  }

  //1-管理员
  static getAdmin() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getInt("isAdmin");
  }

  //推广商品搜索历史本地存储list
  static saveGoodsSearchHistoryListPrefs(List<String> value) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.setStringList(
        'GOODS_SEARCH_HISTORY',
        List.generate(value.length, (index) {
          return value[index].toString();
        }));
  }

  //获取商品搜索历史list
  static Future<List<String>> getGoodsSearchHistoryListPrefs() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getStringList('GOODS_SEARCH_HISTORY') ?? [];
  }

  //保存设置页面推送通知开关状态
  static saveNoticeSwStatus(bool status) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool("NoticeSwStatus", status);
  }

  //获取设置页面推送通知开关状态
  static Future<bool> getNoticeSwStatus() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getBool("NoticeSwStatus") ?? false;
  }

  //保存设置页面个性推送开关状态
  static saveRecommendSwStatus(bool status) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool("RecommendSwStatus", status);
  }

  //获取设置页面个性推送开关状态
  static Future<bool> getRecommendSwStatus() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getBool("RecommendSwStatus") ?? false;
  }


  //获取token
  static getAuthorization() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getString("token") ?? '';
  }

  static setAuthorization(String token) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.setString("token",token);
  }

  //获取token
  static getIndexStatus() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.getInt("indexStatus") ?? '';
  }

  static setIndexStatus(int status) async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return preferences.setInt("indexStatus",status);
  }
}
